package com.boarsoft.message.store.jdbc.pgsql;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.message.store.jdbc.MsgDetailTM;

public class MsgPgsqlDetailTM extends MsgDetailTM {
	private static final Logger log = LoggerFactory.getLogger(MsgPgsqlDetailTM.class);

	public MsgPgsqlDetailTM(String target) {
		super(target);
	}

	/**
	 * 实现针对MySQL的建表语句
	 */
	@Override
	protected void prepareCreate() {
		this.createTblSql = prepareCreateTbl(this.table);
		this.createIdxSql = prepareCreateIdx(this.table);
	}
	
	@Override
	protected String prepareCreateTbl(String table) {
		StringBuilder sb = new StringBuilder() //
				.append("create table ").append(table).append("(\n") //
				.append(" cMessageId char(32) not null\n") //
				.append(" , vRecipient varchar(64) not null\n") //
				.append(" , iAddTime bigint not null\n")//
				.append(" , iLastTime bigint not null default 0\n")//
				.append(" , tStatus smallint not null default 0\n")//
				.append(" , tTried smallint not null default 0\n")//
				.append(" , vMemo varchar(1024)\n")//
				.append(" , primary key(cMessageId, vRecipient)\n")//
				.append(");");
		String createTblSql = sb.toString();
		log.warn("Create table {} with SQL:", table);
		log.warn("{}", createTblSql);
		return createTblSql;
	}

	@Override
	protected String prepareCreateIdx(String table) {
		String createIdxSql = new StringBuilder("create index ")//
				.append(index).append(" on ").append(table)//
				.append("(iAddTime)").toString();
		log.info("Create index {} with SQL:", index);
		log.warn("SQL:\n{}", createIdxSql);
		return createIdxSql;
	}
}
